///////////////////////////////////////////////////////////////////////
// DiscFormatErase.cpp
//
// Written by Nic Wilson

#include "StdAfx.h"
#include "DiscFormatErase.h"
#include "DiscRecorder.h"
#include "DiscFormatEraseEvent.h"

CDiscFormatErase::CDiscFormatErase(void)
: m_discFormatErase(NULL)
, m_mediaTypesArray(NULL)
, m_hResult(0)
, m_hNotificationWnd(NULL)
{
}

CDiscFormatErase::~CDiscFormatErase(void)
{
	if (m_discFormatErase != NULL)
	{
		m_discFormatErase->Release();
	}
}

///////////////////////////////////////////////////////////////////////
//
// CDiscFormatErase::Initialize()
//
// Description:
//		Creates and initializes the IDiscFormat2Erase interface
//
bool CDiscFormatErase::Initialize(CDiscRecorder* pDiscRecorder, const CString& clientName)
{
	ASSERT(m_discFormatErase == NULL);
	ASSERT(pDiscRecorder != NULL);
	if (pDiscRecorder == NULL)
	{
		m_errorMessage = _T("Error - CDiscFormatErase::Initialize - pDiscRecorder is NULL");
		return false;
	}

	// Initialize the IDiscFormat2Erase Interface
	m_hResult = CoCreateInstance(__uuidof(MsftDiscFormat2Erase), NULL, CLSCTX_INPROC_SERVER, __uuidof(IDiscFormat2Erase), (void**)&m_discFormatErase);
	if (!SUCCEEDED(m_hResult))
	{
		m_errorMessage.Format(_T("Unable to Initialize IDiscFormat2Erase - Error:0x%08x"), m_hResult);
		return false;
	}
	// Setup the Disc Format Information
	VARIANT_BOOL isSupported = VARIANT_FALSE;
	m_hResult = m_discFormatErase->IsRecorderSupported(pDiscRecorder->GetInterface(), &isSupported);
	if (isSupported == VARIANT_FALSE)
	{
		m_errorMessage = _T("Recorder not supported");
		return false;
	}

	m_hResult = m_discFormatErase->put_Recorder(pDiscRecorder->GetInterface());
	if (!SUCCEEDED(m_hResult))
	{
		m_errorMessage.Format(_T("IDiscFormat2Erase->put_Recorder Failed - Error:0x%08x"), m_hResult);
		return false;
	}

	m_hResult = m_discFormatErase->put_ClientName(clientName.AllocSysString());
	if (!SUCCEEDED(m_hResult))
	{
		m_errorMessage.Format(_T("IDiscFormat2Erase->put_ClientName Failed - Error:0x%08x"), m_hResult);
		return false;
	}

	m_hResult = m_discFormatErase->get_SupportedMediaTypes(&m_mediaTypesArray);
	if (!SUCCEEDED(m_hResult))
	{
		m_errorMessage.Format(_T("IDiscFormat2Erase->get_SupportedMediaTypes Failed - Error:0x%08x"), m_hResult);
		return false;
	}

	return true;
}

ULONG CDiscFormatErase::GetTotalSupportedMediaTypes()
{
	if (m_mediaTypesArray == NULL)
		return 0;

	return m_mediaTypesArray->rgsabound[0].cElements;
}

int CDiscFormatErase::GetSupportedMediaType(ULONG index)
{
	ASSERT(index < GetTotalSupportedMediaTypes());
	if (index < GetTotalSupportedMediaTypes())
	{
		if (m_mediaTypesArray)
		{
			return ((VARIANT*)(m_mediaTypesArray->pvData))[index].intVal;
		}
	}

	return 0;
}

bool CDiscFormatErase::Erase(HWND hNotificationWnd, BOOL bFull)
{
	if (m_discFormatErase == NULL)
		return false;

	if (hNotificationWnd == NULL)
		return false;

	m_hNotificationWnd = hNotificationWnd;

	// Create the event sink
	CDiscFormatEraseEvent* eventSink = CDiscFormatEraseEvent::CreateEventSink();
	if (eventSink == NULL)
	{
		m_errorMessage = _T("Unable to create event sink");
		return false;
	}

	if (!eventSink->ConnectDiscFormatErase(this))
	{
		m_errorMessage = _T("Unable to connect event sink with interface");
		return false;
	}

	eventSink->SetHwnd(m_hNotificationWnd);
	m_discFormatErase->put_FullErase(bFull);
	m_discFormatErase->EraseMedia();
	delete eventSink;

	if (SUCCEEDED(m_hResult))
	{
		return true;
	}

	m_errorMessage.Format(_T("IDiscFormat2Erase->Write Failed! Error:0x%08x"),
		m_hResult);

	return true;

}

IDiscFormat2Erase* CDiscFormatErase::GetIDiscFormat2ErasePtr()
{
	return m_discFormatErase;
}
